package com.hoshiicloud.market.controller.market;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.market.entity.market.MarketingType;
import com.hoshiicloud.market.param.market.MarketingTypeAddParam;
import com.hoshiicloud.market.param.market.MarketingTypeQueryParam;
import com.hoshiicloud.market.param.market.MarketingTypeUpdateParam;
import com.hoshiicloud.market.service.market.MarketingTypeService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 活动类型 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-05-14
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/marketing/marketing-type")
@Api(value = "营销服务", tags = {"活动类型管理"})
public class MarketingTypeController {
    @Autowired
    private MarketingTypeService marketingTypeService;

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 分页查询活动类型
    */
    @ApiOperation(value = "分页查询活动类型")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid MarketingTypeQueryParam param) {
        try {
            log.info("分页查询活动类型");
            QueryWrapper<MarketingType> queryWrapper = new QueryWrapper<MarketingType>();
            queryWrapper.eq(StringUtils.isNotBlank(param.getTypename()), "typename", param.getTypename());
            queryWrapper.eq(param.getDeleted() != null, "deleted", param.getDeleted());
            IPage<MarketingType> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            queryWrapper.select(BeanUtils.getSqlSelect(MarketingType.class));
            return Response.data(marketingTypeService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询活动类型异常", e);
            return Response.failed("分页查询活动类型异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 根据id查询活动类型
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询活动类型")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(marketingTypeService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增活动类型")
    public Response save(@RequestBody @Valid MarketingTypeAddParam param) {
        try {
            MarketingType data = BeanUtils.covertType(param, MarketingType.class);
            return Response.data(marketingTypeService.save(data));
        } catch (ServiceException e) {
            log.error("新增活动类型异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("新增活动类型异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 修改活动类型
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改活动类型")
    public Response update(@RequestBody @Valid MarketingTypeUpdateParam param) {
        try {
            MarketingType data = BeanUtils.covertType(param, MarketingType.class);
            return Response.data(marketingTypeService.updateById(data));
        }catch (ServiceException e) {
            log.error("修改活动类型异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("修改活动类型异常", e);
            return Response.failed();
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除活动类型
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除活动类型")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        try {
            MarketingType data=marketingTypeService.getById(id);
            if(data==null){
                log.error("删除活动类型异常，无法找到活动类型记录");
                return Response.failed("删除活动类型异常，无法找到活动类型记录");
            }
            marketingTypeService.deleteById(data);
            return Response.success("删除活动类型成功");
        }catch (ServiceException e) {
            log.error("删除活动类型异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("删除活动类型异常", e);
            return Response.failed();
        }
    }

}
